<?php
/**
 * 任务id：553
 * 任务内容：【后台需求】用户在心愿单界面预约到店后，后台可查看用户预约到店时心愿单商品
 * Created by Zend Studio.
 * User: zenan.li (FDG348)
 * Date: 2018/3/23
 * Time: 10:00
 */
namespace backend\controllers;

use Yii;
use yii\web\AdminController;
use backend\models\UploadForm;
use backend\models\ShareWishReserv;
use backend\models\ShareWishReservLog;
use backend\models\ShareWishList;
use common\helps\tools;
use common\models\GoodsList;

class WishSingleReportController extends AdminController
{
    public $layout = false;
    //业务关键字
    public $service = 'WishSingleReport';
    
    public function actionIndex()
    {
        $model = new UploadForm();
        return $this->render('index',['model'=>$model]);
    }
    
    public function actionGetWishSingleReport(){
        $data = Yii::$app->request->post();
        $cust_member_no = trim($data['cust_member_no']);
        $use_flg = $data['use_flg'];
        $reserv_name = trim($data['reserv_name']);
        $reserv_mobile = trim($data['reserv_mobile']);
        $where = [];
        $where[] = 'and';
        $where[] = ['j_share_wish_list.type'=>2];
        //$query = ShareWishReserv::find();
        $query = ShareWishList::find()->joinWith('shareWishReserv')->select('j_share_wish_reserv.*');
        if ($cust_member_no){
            $where[] = ['like','j_share_wish_reserv.cust_member_no',$cust_member_no];
        }
        if ($use_flg || $use_flg === '0'){
            $where[] = ['j_share_wish_reserv.use_flg'=>$use_flg];
        }
        if ($reserv_name){
            $where[] = ['like','j_share_wish_reserv.reserv_name',$reserv_name];
        }
        if ($reserv_mobile){
            $where[] = ['like','j_share_wish_reserv.reserv_mobile',$reserv_mobile];
        }
        $query = $query->where($where);
        $count = $query->count();
        $list = $query->offset($data['start'])->orderBy('j_share_wish_reserv.id asc')->limit($data['length'])->asArray()->all();
        $result['draw'] = intval($data['draw']);
        $result['recordsTotal'] = intval($count);
        $result['recordsFiltered'] = intval($count);
        $result['data'] = $list;
        return \yii\helpers\Json::encode($result);
    }
    
    public function actionChangeWishSingleReportStatus(){
        $model = new ShareWishReserv();
        $log_model = new ShareWishReservLog();
        $data = Yii::$app->request->post();
        $id = $data['id'];
        $use_flg = $data['use_flg'];
        $data_new = ['use_flg'=>$use_flg];
        $transaction = Yii::$app->db->beginTransaction();
        try {
            $ret = $model::updateAll($data_new,['id'=>$id]);
            if(!$ret){
                throw new \Exception('操作失败');
            }
            //修改预约门店处理状态写入日志
            $log_model->writeWishSingleReportLog($id,ShareWishReservLog::TYPE_LOG,ShareWishReservLog::LOG_TYPE_USE_FLG,$use_flg);
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollBack();
            $this->retJson($e->getMessage(),500);
        }
        $this->retJson('操作成功',200);
    }
    
    public function actionGetWishSingleReportRecord(){
        $data = Yii::$app->request->post();
        $id = $data['share_wish_reserv_id'];
        $record = ShareWishReservLog::find()->where('share_wish_reserv_id=:share_wish_reserv_id and type='.ShareWishReservLog::TYPE_RECORD,[
            ':share_wish_reserv_id'=>$id
        ])->asArray()->all();
        $this->retJson($record,200);
    }
    
    public function actionWriteWishSingleReportRecord(){
        $data = Yii::$app->request->post();
        $id = $data['id'];
        $record = $data['record'];
        $log_model = new ShareWishReservLog();
        //增加联系记录写入日志
        $log_model->writeWishSingleReportLog($id,ShareWishReservLog::TYPE_RECORD,'','',$record);
        $this->retJson($record,200);
    }
    
    public function actionLog(){
        $data = Yii::$app->request->get();
        return $this->render('log',['id'=>intval($data['share_wish_reserv_id'])]);
    }
    
    public function actionSearchLog(){
        $data = Yii::$app->request->post();
        $id = $data['id'];
        $reservationtime = $data['reservationtime'];
        $content = trim($data['content']);
        $oper = trim($data['oper']);
        $page = $data['paging'];
        $where = [];
        $where[] = 'and';
        if ($reservationtime){
            $reservationtime_arr = explode(' - ', $reservationtime);
            if (count($reservationtime_arr) == 2){
                $reservationtime_arr[0] = date('Y-m-d',strtotime($reservationtime_arr[0]));
                $reservationtime_arr[1] = date('Y-m-d',strtotime($reservationtime_arr[1])+86400);
                $where[] = ['between','oper_time',$reservationtime_arr[0],$reservationtime_arr[1]];
            }
        }
        if ($content){
            $where[] = ['like','content',$content];
        }
        if ($oper){
            $where[] = ['like','oper',$oper];
        }
        $where[] = ['share_wish_reserv_id'=>$id];
        $where[] = ['type'=>ShareWishReservLog::TYPE_LOG];
        $rows_perpage = 20;
        $page = ($page - 1) * $rows_perpage;
        $log = ShareWishReservLog::find()->where($where)->offset($page)->limit($rows_perpage)->asArray()->all();
        $this->retJson($log, 200);
    }
    
    public function actionGetGoods(){
        $data = Yii::$app->request->post();
        $id = $data['share_wish_reserv_id'];
        $share_wish = ShareWishList::find()->where('id=:id',[':id'=>$id])->asArray()->one();
        $goods = [];
        if (tools::arrayHasValue($share_wish,'goods_ids')){
            $goods = $share_wish['goods_ids'] ? json_decode($share_wish['goods_ids'],true) : '';
            if (is_array($goods) && count($goods) > 0)foreach ($goods as &$goods_val){
                $goods_val['activity_new']	= GoodsList::getGoodsListActivity($goods_val['main_goods_id'],false);
                $use_flg_new = GoodsList::find()->select('use_flg')->where('main_goods_id='.$goods_val['main_goods_id'])->asArray()->one();
                $goods_val['use_flg_new'] = tools::arrayHasValue($use_flg_new,'use_flg') ? $use_flg_new['use_flg'] : 0;
            }
        }
        $this->retJson($goods, 200);
    }
    
    public function actionExportWishSingleReport(){
        $log_model = new ShareWishReservLog();
        $data = Yii::$app->request->post();
        $share_wish_reserv_id = explode(',', $data['share_wish_reserv_id']);
        if (count($share_wish_reserv_id) == 0){
            $this->retJson('请选择需要导出的预约信息', 500);
        }
        $select = [
            'j_share_wish_reserv.id',
            'j_share_wish_reserv.wish_list_id',
            'j_share_wish_reserv.cust_member_no',
            'j_share_wish_reserv.create_time',
            'j_share_wish_reserv.reserv_name',
            'j_share_wish_reserv.reserv_mobile',
            'j_share_wish_reserv.reserv_time',
            'j_share_wish_reserv.reserv_time_ex',
            'j_share_wish_reserv.use_flg',
            'j_share_wish_list.goods_ids'
        ];
        $result_data = ShareWishReserv::find()->select(implode(',', $select))->joinWith('shareWishList')->where(['j_share_wish_reserv.id'=>$share_wish_reserv_id])->asArray()->all();
        $export_val = [];
        $i = 0;
        if (is_array($result_data) && count($result_data) > 0)foreach ($result_data as $val){
            $goods = $val['goods_ids'] ? json_decode($val['goods_ids'],true) : '';
            if (is_array($goods) && count($goods) > 0)foreach ($goods as $goods_val){
                $activity_new	= GoodsList::getGoodsListActivity($goods_val['main_goods_id'],false);
                $use_flg_new = GoodsList::find()->select('use_flg')->where('main_goods_id='.$goods_val['main_goods_id'])->asArray()->one();
                $use_flg_new = tools::arrayHasValue($use_flg_new,'use_flg') ? $use_flg_new['use_flg'] : 0;
                unset($val['wish_list_id'],$val['goods_ids'],$val['shareWishList']);
                $export_val[$i] = $val;
                $export_val[$i]['sku'] = $goods_val['sku'];
                $export_val[$i]['goods_name_cn'] = $goods_val['goods_name_cn'];
                $export_val[$i]['goods_create_time'] = $goods_val['create_time'];
                $export_val[$i]['use_flg'] = $goods_val['use_flg'] == '1' ? '有效' : '无效';
                $activity_info = $goods_val['activity_info'];
                $export_val[$i]['activity_id'] = count($activity_info) > 0 ? implode('；', array_column($activity_info, 'id')) : '';
                $export_val[$i]['activity_identification'] = count($activity_info) > 0 ? implode('；', array_column($activity_info, 'activity_identification')) : '';
                $export_val[$i]['new_use_flg'] = $use_flg_new == '1' ? '有效' : '无效';
                $export_val[$i]['new_activity_id'] = count($activity_new) > 0 ? implode('；', array_column($activity_new, 'id')) : '';
                $export_val[$i]['new_activity_identification'] = count($activity_new) > 0 ? implode('；', array_column($activity_new, 'activity_identification')) : '';
                $export_val[$i]['is_handle'] = $val['use_flg'] == '1' ? '是' : '否';
                $i++;
            }else{
                unset($val['wish_list_id'],$val['goods_ids'],$val['shareWishList']);
                $export_val[$i] = $val;
                $export_val[$i]['sku'] = '';
                $export_val[$i]['goods_name_cn'] = '';
                $export_val[$i]['goods_create_time'] = '';
                $export_val[$i]['use_flg'] = '';
                $export_val[$i]['activity_id'] = '';
                $export_val[$i]['activity_identification'] = '';
                $export_val[$i]['new_use_flg'] = '';
                $export_val[$i]['new_activity_id'] = '';
                $export_val[$i]['new_activity_identification'] = '';
                $export_val[$i]['is_handle'] = $val['use_flg'] == '1' ? '是' : '否';
                $i++;
            }
            //修改预约门店处理状态写入日志
            $log_model->writeWishSingleReportLog($val['id'],ShareWishReservLog::TYPE_LOG,ShareWishReservLog::LOG_TYPE_EXPORT_EXCEL);
        }
        $filed = [
            'id'=>'序号',
            'cust_member_no'=>'会员账号',
            'create_time'=>'预约门店提交时间',
            'reserv_name'=>'顾客姓名',
            'reserv_mobile'=>'预约联系电话',
            'reserv_time'=>'预约日期',
            'reserv_time_ex'=>'预约时段',
            'sku'=>'商品编码',
            'goods_name_cn'=>'商品名称',
            'goods_create_time'=>'商品添加心愿单时间',
            'use_flg'=>'预约门店时商品状态',
            'activity_id'=>'预约门店时促销ID',
            'activity_identification'=>'预约门店时活动标识',
            'new_use_flg'=>'现在商品状态',
            'new_activity_id'=>'现在促销ID',
            'new_activity_identification'=>'现在活动标识',
            'is_handle'=>'已处理'
        ];
        $width = [
            '序号'=>10,
            '会员账号'=>20,
            '预约门店提交时间'=>20,
            '顾客姓名'=>15,
            '预约联系电话'=>15,
            '预约日期'=>15,
            '预约时段'=>20,
            '商品编码'=>30,
            '商品名称'=>30,
            '商品添加心愿单时间'=>20,
            '预约门店时商品状态'=>10,
            '预约门店时促销ID'=>15,
            '预约门店时活动标识'=>30,
            '现在商品状态'=>10,
            '现在促销ID'=>15,
            '现在活动标识'=>30,
            '已处理'=>10
        ];
        $type = [
            'cust_member_no'=>'setCellValueExplicit'
        ];
        $filename = 'wish_single_export'.date('YmdHis').'.xls';
        $filename = iconv("utf-8", "gb2312", $filename);
        tools::exportExcel($filed, $export_val, $filename,$width,$type);
    }
}